﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySoft.Data;

namespace Caifu.Datas
{
	/// <summary>
	/// 基金信息
	/// </summary>
	public class FundData
	{
		/// <summary>
		/// 
		/// </summary>
		public static readonly FundData Instance = new FundData();

		private readonly DbSession dbSession = new DbSession("Caifu");

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fund"></param>
		public void AddFund(Fund fund)
		{
			AddFund(fund, null);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fund"></param>
		/// <param name="features"></param>
		public void AddFund(Fund fund, string[] features)
		{
			if (String.IsNullOrEmpty(fund.Code))
			{
				throw new CaifuException("基金代码不能为空");
			}
			if (this.ExistsFundCode(fund.Code))
			{
				throw new CaifuException("存在相同的基金代码");
			}

			fund.Detach();
			if (fund.Id == Guid.Empty) fund.Id = Itfort.Comb.NewComb();
			if (fund.AddTime == DateTime.MinValue) fund.AddTime = DateTime.Now;
			if (fund.UpdateTime == DateTime.MinValue) fund.UpdateTime = DateTime.Now;
			dbSession.Save<Fund>(fund);
			CategoryFeatureData.Instance.SetCategoryFeatures(fund.Id, features);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fund"></param>
		public void UpdateFund(Fund fund)
		{
			UpdateFund(fund, null);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fund"></param>
		/// <param name="features"></param>
		public void UpdateFund(Fund fund, string[] features)
		{
			fund.Attach();
			fund.UpdateTime = DateTime.Now;
			dbSession.Save<Fund>(fund);
			CategoryFeatureData.Instance.SetCategoryFeatures(fund.Id, features);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="id"></param>
		public void DeleteFund(Guid id)
		{
			dbSession.Delete<Fund>(id);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public Fund GetFund(Guid id)
		{
			return dbSession.Single<Fund>(id);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundCode"></param>
		/// <returns></returns>
		public Fund GetFund(string fundCode)
		{
			return dbSession.Single<Fund>(Fund._.Code == fundCode);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public IList<Fund> GetFunds(Int32 pageIndex, Int32 pageSize)
		{
			return dbSession.From<Fund>()
				.Where(Fund._.FundState != (int)FundState.Delisting)
				.OrderBy(Fund._.Established.Desc)
				.GetPage(pageSize).ToList(pageIndex);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="type"></param>
		/// <param name="pageIndex"></param>
		/// <param name="pageSize"></param>
		/// <returns></returns>
		public IList<Fund> GetFunds(FundType type, Int32 pageIndex, Int32 pageSize)
		{
			return dbSession.From<Fund>()
				.Where(Fund._.FundType == (int)type)
				.OrderBy(Fund._.Established.Desc)
				.GetPage(pageSize).ToList(pageIndex);
		}

		/// <summary>
		/// 基金代码是否已经存在。
		/// </summary>
		/// <param name="fundCode">基金代码。</param>
		/// <returns>
		/// 如果存在，返回true；否则，返回false。
		/// </returns>
		public bool ExistsFundCode(string fundCode)
		{
			return dbSession.Exists<Fund>(Fund._.Code == fundCode);
		}
	}
}

